package logic

import (
	"bw-renting/order-rpc/orderclient"
	"context"
	"strconv"

	"bw-renting/api/internal/svc"
	"bw-renting/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type GenerateOrderLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGenerateOrderLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GenerateOrderLogic {
	return &GenerateOrderLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

// TODO: 创建订单
func (l *GenerateOrderLogic) GenerateOrder(req *types.GenerateOrderReq) (resp *types.Data, err error) {
	// todo: add your logic here and delete this line
	//Todo：调用订单rpc服务
	order, err := l.svcCtx.OrderServer.CreateOrder(l.ctx, &orderclient.CreateOrderReq{
		UserId:    int64(req.UserId),
		HouseId:   int64(req.HouseId),
		Title:     req.Title,
		StartDate: int64(req.StartDate),
		EndDate:   int64(req.EndDate),
		Price:     float32(req.Price),
	})
	if err != nil {
		return &types.Data{Code: -1, Message: "创建订单失败RPC返回失败", Data: map[string]string{
			"Error": err.Error(),
		}}, err
	}
	return &types.Data{Code: 200, Message: "success", Data: map[string]string{
		"Order_Id": strconv.Itoa(int(order.OrderId)),
	}}, nil
	return
}
